﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * POP3 Parser Class
 *      Parses the standard POP3 text into the POP3 protocol objects.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using Adaptive.Foundation;
using Adaptive.Foundation.Network.Cnr;

namespace Adaptive.Foundation.Network.Pop3
{
    /// <summary>
    /// Parses the standard POP3 text into the POP3 protocol objects.
    /// </summary>
    public sealed class Pop3Parser : CnrDataParserBase
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public Pop3Parser()
            : base()
        {
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Parses the specified command string into a protocol-specific object graph.
        /// </summary>
        /// <param name="originalCommandString">
        /// The command string to be parsed.
        /// </param>
        /// <returns>
        /// A <see cref="CnrCommandBase"/> instance representing the text that was parsed.
        /// </returns>
        public override CnrCommandBase ParseProtocolCommandString(string originalCommandString)
        {
            return null;
        }
        /// <summary>
        /// Parses the specified response string into a protocol-specific object graph.
        /// </summary>
        /// <param name="originalResponseString">
        /// The response string to be parsed.
        /// </param>
        /// <returns>
        /// A <see cref="CnrResponseBase"/> instance representing the text that was parsed.
        /// </returns>
        public override CnrResponseBase ParseProtocolResponseString(string originalResponseString)
        {
            Pop3Response returnValue = null;        //Return value.
            string[] rawContent = null;             //Raw component content.

            rawContent = ParseProtocolDataIntoParts(originalResponseString);
            returnValue = new Pop3Response();
            returnValue.ResponseData = rawContent[1];
            return returnValue;
        }
        /// <summary>
        /// Parses the provided content into component parts based on a protocol-specific definition.
        /// </summary>
        /// <param name="originalDataString">The text content to be parsed.</param>
        /// <returns>
        /// A <see cref="String"/> array containing the protocol-specific component parts.
        /// </returns>
        public override string[] ParseProtocolDataIntoParts(string originalDataString)
        {
            string[] returnValue = null;
            int characterIndex = -1;

            //Remove the carriage return and/or line feed characters.
            originalDataString = originalDataString.Replace("\r", string.Empty);
            originalDataString = originalDataString.Replace("\n", string.Empty);

            //Create return container.
            returnValue = new string[2];

            //Seperate the response code from the message text.
            characterIndex = originalDataString.IndexOf(" ");
            if (characterIndex == -1)
            {
                //A response code (such as +OK or -ERR) with no accompanying text.
                returnValue[0] = originalDataString;
                returnValue[1] = string.Empty;
            }
            else
            {
                //A response code (such as +OK or -ERR) with accompanying text.
                returnValue[0] = originalDataString.Substring(0, characterIndex);
                returnValue[1] = originalDataString.Substring(characterIndex+1, originalDataString.Length - (characterIndex + 1));
            }
            return returnValue;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalParameterData"></param>
        /// <returns></returns>
        public override string GetParameterName(string originalParameterData)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalParameterData"></param>
        /// <returns></returns>
        public override object GetParameterValue(string originalParameterData)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}